package com.aixin.project.data;

import com.aixin.project.conf.Configure;
import com.aixin.project.handler.CourseOrderHandler;
import com.aixin.project.handler.ExpertHandler;
import com.aixin.project.util.AxMemcached;
import com.aixin.project.util.AxUtil;
import com.aixin.project.util.DateUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;

import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Sufeng
 * Date: 12-4-14
 * Time: 下午6:00
 * To change this template use File | Settings | File Templates.
 */
public class Course implements Serializable {
    private long id;
    private String title;
    private String description;
    private long expertid;
    private Date starttime;
    private Date endtime;
    private Date createtime;
    private Double pointprice;
    private Double addprice;
    private Double baseprice;
    private Double totalprice;
    private int point;
    private int usernum;
    private String expertname;
    private String expertdes;
    private long coid;
    private String service;
    private int coursetype; //1直播  ，2录播 ,3点播

    private String genseeid; //直播地址
    private String organizerurl; //直播地址
    private String weburl; //直播地址
    private String clienturl; //主讲人地址
    private String zzzpass;
    private String jbpass;
    private String cyzpass;

    public String getGenseeid() {
        return genseeid;
    }

    public void setGenseeid(String genseeid) {
        this.genseeid = genseeid;
    }

    public String getOrganizerurl() {
        return organizerurl;
    }

    public void setOrganizerurl(String organizerurl) {
        this.organizerurl = organizerurl;
    }

    public String getWeburl() {
        return weburl;
    }

    public void setWeburl(String weburl) {
        this.weburl = weburl;
    }

    public String getClienturl() {
        return clienturl;
    }

    public void setClienturl(String clienturl) {
        this.clienturl = clienturl;
    }

    public String getZzzpass() {
        return zzzpass;
    }

    public void setZzzpass(String zzzpass) {
        this.zzzpass = zzzpass;
    }

    public String getJbpass() {
        return jbpass;
    }

    public void setJbpass(String jbpass) {
        this.jbpass = jbpass;
    }

    public String getCyzpass() {
        return cyzpass;
    }

    public void setCyzpass(String cyzpass) {
        this.cyzpass = cyzpass;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public int getCoursetype() {
        return coursetype;
    }

    public void setCoursetype(int coursetype) {
        this.coursetype = coursetype;
    }

    public Double getTotalprice() {
        return totalprice;
    }

    public void setTotalprice(Double totalprice) {
        this.totalprice = totalprice;
    }

    public long getCoid() {
        return coid;
    }

    public void setCoid(long coid) {
        this.coid = coid;
    }

    public String getExpertname() {
        if (expertname == null || expertname.length() == 0) {
            Expert expert = ExpertHandler.find(this.expertid);
            expertname = (expert==null?"":expert.getName());
        }
        return expertname;
    }

    public void setExpertname(String expertname) {
        this.expertname = expertname;
    }

    public int getUsernum() {
        return usernum;
    }

    public void setUsernum(int usernum) {
        this.usernum = usernum;
    }

    public int getPoint() {
        return point;
    }

    public void setPoint(int point) {
        this.point = point;
    }


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public Double getPointprice() {
        return pointprice;
    }

    public void setPointprice(Double pointprice) {
        this.pointprice = pointprice;
    }

    public Double getAddprice() {
        return addprice;
    }

    public void setAddprice(Double addprice) {
        this.addprice = addprice;
    }

    public Double getBaseprice() {
        return baseprice;
    }

    public void setBaseprice(Double baseprice) {
        this.baseprice = baseprice;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public long getExpertid() {
        return expertid;
    }

    public void setExpertid(long expertid) {
        this.expertid = expertid;
    }

    public Date getStarttime() {
        return starttime;
    }

    public void setStarttime(Date starttime) {
        this.starttime = starttime;
    }

    public Date getEndtime() {
        return endtime;
    }

    public void setEndtime(Date endtime) {
        this.endtime = endtime;
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }


    public String getStatus() {
        long hour = 60 * 60 * 1000;
        Date now = new Date();
        long nowtimeL = now.getTime();
        long pretimeL = starttime.getTime() - hour * Configure.LIVECOURSE_TIME;
        long starttimeL = starttime.getTime();
        long endtimeL = endtime.getTime();

        if (nowtimeL > pretimeL && nowtimeL <= endtimeL)
            return "进行中";
        else if (nowtimeL <= pretimeL)
            return "未开始";
        else
            return "已结束";


/*        int hours = DateUtil.compareToHours(getStarttime());
        if (hours >= 0 && hours < Configure.LIVECOURSE_TIME) {
            return "进行中";
        } else if (hours >= Configure.LIVECOURSE_TIME) return "未开始";
        else if (hours == -1) return "已结束";

        return "未开始";*/
    }

    public String getStatusImg() {
        String status = getStatus();
        if ("进行中".equals(status))
            return "zb.gif";
        else if ("未开始".equals(status))
            return "wks.gif";
        else
            return "yjs.gif";
/*        int hours = DateUtil.compareToHours(getStarttime());
        if (hours > Configure.LIVECOURSE_TIME) return "wks.gif";
        else if (hours == -1) return "yjs.gif";
        else if (hours == 0 || hours == Configure.LIVECOURSE_TIME) return "zb.gif";
        return "wks.gif";*/
    }

    public String getDay() {
        return DateFormatUtils.format(this.getStarttime(), "MM月dd日");
    }

    public String getTimeDuration() {
        return DateFormatUtils.format(this.getStarttime(), "HH:mm") + "-" + DateFormatUtils.format(this.getEndtime(), "HH:mm");
    }

    @Override
    public String toString() {
        return "Course{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", description='" + description + '\'' +
                ", expertid=" + expertid +
                ", starttime=" + starttime +
                ", price=" + pointprice +
                ", endtime=" + endtime +
                ", createtime=" + createtime +
                '}';
    }

/*    public static void main(String[] args) throws InterruptedException, ParseException {
        Date date = DateUtils.parseDate("2012044-221000", new String[]{"yyyyMMdd-HHmmss"});
        System.out.println(date.getTime());
        System.out.println(System.currentTimeMillis());
        System.out.println(new Date().getTime());
        Thread.sleep(500);
        System.out.println(new Date().getTime());
    }*/

    public String getExpertdes() {
        if (expertdes == null) {
            Expert expert = ExpertHandler.find(this.expertid);
            expertdes = (expert == null ? null : expert.getDescription());
        }
        return expertdes;
    }

    public void setExpertdes(String expertdes) {
        this.expertdes = expertdes;
    }

    public String shortExpertDesc(int num) {
        if (expertdes == null) {
            Expert expert = ExpertHandler.find(this.expertid);
            expertdes = (expert == null ? null : expert.getDescription());
        }
        if (expertdes != null && expertdes.length() > num) {
            return expertdes.substring(0, num) + "...";
        }
        return expertdes;
    }

    public String shortDesc(int num) {
        if (description != null && description.length() > num) {
            return description.substring(0, num) + "...";
        }
        return description;
    }

    public String secretWeburl() {
        StringBuffer ret = null;
        if (StringUtils.trimToNull(weburl) != null) {
            AxSession axsession = AxUtil.getAxSession();
            long genseeuid = axsession.getGenseeuid();
            long uid = axsession.getUid();
            int pointcount = CourseOrderHandler.pointCount(uid, this.id);
            if (pointcount <= 0)
                return "full";
            String key = CommonConstants.MKEY_LIVEBROAD_GENSEEUID + this.id + "_" + uid;
            List<String> joinlistKey = (List<String>) AxMemcached.get(CommonConstants.MKEY_LIVEBROAD_JOINLIST + uid);
            if (joinlistKey == null)
                joinlistKey = new ArrayList<String>();


            List<Long> joinList = (List<Long>) AxMemcached.get(key);
            if (joinList == null)
                joinList = new ArrayList<Long>();
            int pointCount = CourseOrderHandler.pointCount(uid, this.id);
            if (!joinList.contains(genseeuid) && joinList.size() < pointCount) { //不包含该用户，未达到上限，可以听课
                joinList.add(genseeuid);
                if (!joinlistKey.contains(key)){
                    joinlistKey.add(key);
                    AxMemcached.set(CommonConstants.MKEY_LIVEBROAD_JOINLIST + uid,joinlistKey,new Date(CommonConstants.MEMCACHED_SESSION_TIMEOUT_DAY*1000));
                }

            } else if (joinList.contains(genseeuid)) {
                if (!joinlistKey.contains(key)){
                    joinlistKey.add(key);
                    AxMemcached.set(CommonConstants.MKEY_LIVEBROAD_JOINLIST + uid,joinlistKey,new Date(CommonConstants.MEMCACHED_SESSION_TIMEOUT_DAY*1000));
                }
            } else {
                return "full";
            }

            String k = AxUtil.base64Encode(uid + "_" + this.id + "_" + System.currentTimeMillis());
            AxMemcached.set(k, "1", new Date(CommonConstants.MEMCACHED_SESSION_TIMEOUT_HOUR * 1000));
            AxMemcached.set(key, joinList, new Date(CommonConstants.MEMCACHED_SESSION_TIMEOUT_HOUR * 5 * 1000));
            ret = new StringBuffer(weburl);
            int index = weburl.indexOf("?");
            if (index == -1) {
                ret.append("?token=").append(cyzpass);
                ret.append("&sec=md5");
            }
            ret.append("&sd=0").append("&uid=").append(genseeuid).append("&k=").append(k);
        }
        return ret == null ? null : ret.toString();
    }

    public static void main(String[] args) {
        Base64 base64 = new Base64();
        String str = "33333-55555";
        String encodeStr = AxUtil.base64Encode(str);
        String decodeStr = AxUtil.base64Decode(encodeStr);

//        enbytes = base64.encode(str.getBytes());
//        encodeStr = new String(enbytes);
//        debytes = base64.decode(enbytes);
//        decodeStr = new String(debytes);

        System.out.println("编码前:" + str);
        System.out.println("编码后:" + encodeStr);
        System.out.println("解码后:" + decodeStr);
    }
}
